package com.easy.product.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.easy.common.annotation.LogRecord;
import com.easy.common.bean.Product;
import com.easy.common.service.IProductService;
import com.easy.common.util.ResultData;
import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("product")
@CrossOrigin
public class ProductController {

    @Autowired
    IProductService productSer;
    @PostMapping("/")
    public ResultData insert(@RequestBody Product product){
        Product data= productSer.insert(product);
        System.out.println(data);
        return ResultData.success(data);
    }
    @PutMapping("/")
    @LogRecord(module = "商品模块",info="修改信息")
    public ResultData update(@RequestBody Product product){
        Product data=productSer.update(product);
        return ResultData.success(data);
    }
    @DeleteMapping("/{id}")
    public ResultData delete(@PathVariable Integer id){
        int reuslt=productSer.delete(id);
        if(reuslt>=1){
            return ResultData.success("200","删除成功",null);
        }else{
            return ResultData.fail();
        }
    }
    @GetMapping("/{id}")
//    @PreAuthorize("hasAnyAuthority('user:list')")
    public ResultData getByID(@PathVariable Integer id){
        Product product=productSer.getByID(id);
        return ResultData.success(product);
    }
    @GetMapping("/page")
    public ResultData getListByPage(Page page, Product product){
        IPage<Product> data=productSer.getListByPage(page,product);
        return ResultData.success(data);
    }
    @GetMapping("/list")
//    @PreAuthorize("hasAnyAuthority('user:page')")
    public ResultData getList(Product product){
        System.out.println("---------------getList");
        List<Product> data=productSer.getList(product);
        return ResultData.success(data);
    }
}
